package com.zkt.recommend.application.mq;

import com.zkt.recommend.application.entity.SubjectUserLabel;
import com.zkt.recommend.domain.algorithm.UserPortraitCalculator;
import com.zkt.recommend.infra.entity.SubjectUserRecord;
import com.zkt.recommend.infra.basic.entity.RecommendUserPortrait;
import com.zkt.recommend.infra.basic.service.IRecommendUserPortraitService;
import com.zkt.recommend.infra.rpc.SubjectRPC;
import com.zkt.subject.entity.SubjectUserRecordDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/25
 * @description
 **/

@Component
@RocketMQMessageListener(topic = "deal-new-user-label", consumerGroup = "consumer-userLabel")
@Slf4j
public class DealNewUserLabel implements RocketMQListener<SubjectUserLabel> {
	
	@Resource
	private UserPortraitCalculator userPortraitCalculator;
	
	@Resource
	private IRecommendUserPortraitService recommendUserPortraitService;
	
	@Resource
	private SubjectRPC subjectRPC;
	
	@Override
	public void onMessage(SubjectUserLabel message) {
		if (log.isInfoEnabled()) {
			log.info("处理用户增加新的标签：{}", message.toString());
		}
		
		SubjectUserRecordDTO subjectUserRecordDTO = new SubjectUserRecordDTO();
		subjectUserRecordDTO.setUserId(message.getUserId());
		subjectUserRecordDTO.setLabelId(message.getLabelId());
		
		List<SubjectUserRecord> allUserRecord = subjectRPC.getAllUserRecord(subjectUserRecordDTO);
		
		RecommendUserPortrait newRecommendUserPortrait = userPortraitCalculator.calculateUserPortrait(message.getUserId(),
				message.getLabelId(), allUserRecord);
		
		recommendUserPortraitService.save(newRecommendUserPortrait);
	}
}
